package com.hema.auction.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hema.auction.R;
import com.hema.auction.activity.ConfirmOrderActivity;
import com.hema.auction.activity.GoodsDetailsActivity;
import com.hema.auction.activity.OrderDetailActivity;
import com.hema.auction.activity.PulishSunActivity;
import com.hema.auction.adapter.DifferenceOrderAdapter;
import com.hema.auction.base.BaseFragment;
import com.hema.auction.bean.GoodsInfo;
import com.hema.auction.http.HttpTag;
import com.hema.auction.http.biz.HttpManager;
import com.hema.auction.utils.Constant;
import com.hema.auction.widget.view.EmptyView;
import com.hema.auction.widget.view.MyLoadMoreView;
import com.hema.auction.widget.view.RefreshRecyclerView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;

/**
 * 差价购买订单
 * author by john
 */

public class DifferenceOrderFragment extends BaseFragment {

    @BindView(R.id.refresh_recycler_view)
    RefreshRecyclerView recyclerView;
    private DifferenceOrderAdapter differenceOrderAdapter;
    private List<GoodsInfo> infos = new ArrayList<>();
    private int position;


    @Override
    protected int getLayout() {
        return R.layout.fragment_sun_square;
    }

    public static DifferenceOrderFragment newInstance(int position) {
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        DifferenceOrderFragment fragment = new DifferenceOrderFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void init() {
        super.init();
        position = getArguments().getInt("position");
        recyclerView.setVerLayoutManager();
        differenceOrderAdapter = new DifferenceOrderAdapter(infos);
        differenceOrderAdapter.setLoadMoreView(new MyLoadMoreView());
        differenceOrderAdapter.setOnLoadMoreListener(this, recyclerView.getRecyclerView());
        differenceOrderAdapter.setEnableLoadMore(true);
        EmptyView emptyView = new EmptyView(getActivity());
        emptyView.setViewType(EmptyView.OTHER);
        differenceOrderAdapter.setEmptyView(emptyView);
        recyclerView.setAdapter(differenceOrderAdapter);
        recyclerView.setOnRefreshListener(this);
        recyclerView.setRefresh(true);
        differenceOrderAdapter.setOnLoadMoreListener(this, recyclerView.getRecyclerView());
        differenceOrderAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                GoodsInfo item = differenceOrderAdapter.getItem(position);
                Bundle bundle = new Bundle();
                bundle.putParcelable(Constant.EXTRA_GOOD_INFO, item);
                intentTo(GoodsDetailsActivity.class, bundle);
            }
        });
        differenceOrderAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                TextView tv = (TextView) view.findViewById(view.getId());
                GoodsInfo item = differenceOrderAdapter.getItem(position);
                switch (tv.getText().toString()) {
                    case "立即付款":
                        Bundle bundle = new Bundle();
                        bundle.putBoolean(Constant.EXTRA_CJ_BUY, true);
                        bundle.putBoolean(Constant.EXTRA_CJ_BUY_PAY, true);
                        bundle.putString(Constant.EXTRA_ORDER_ID, item.getOrderId());
                        intentTo(ConfirmOrderActivity.class, bundle);
                        break;
                    case "确认签收":
                        showProgressDialog();
                        HttpManager.getInstance(getActivity()).sureOrder(item.getOrderId(), DifferenceOrderFragment.this);
                        break;
                    case "我要晒单":
                        bundle = new Bundle();
                        bundle.putParcelable(Constant.EXTRA_GOOD_INFO, item);
                        intentTo(PulishSunActivity.class, bundle);
                        break;
                    case "查看详情":
                        bundle = new Bundle();
                        bundle.putBoolean(Constant.EXTRA_CJ_BUY, true);
                        bundle.putParcelable(Constant.EXTRA_GOOD_INFO, item);
                        intentTo(OrderDetailActivity.class, bundle);
                        break;
                }
            }
        });
    }

    public void setRefresh() {
        recyclerView.setRefresh(true);
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        pageIndex = 1;
        HttpManager.getInstance(getActivity()).getCjOrders(pageIndex, pageSize, position, this);
    }


    @Override
    protected void onLoadMore() {
        super.onLoadMore();
        pageIndex++;
        HttpManager.getInstance(getActivity()).getCjOrders(pageIndex, pageSize, position, this);
    }

    @Override
    protected void onSuccess(HttpTag httpTag, Call call, JSONObject result) {
        super.onSuccess(httpTag, call, result);
        switch (httpTag) {
            case TAG_GET_CJ_ORDERS:
                try {
                    if (result.getInt("errcode") == 0) {
                        List<GoodsInfo> newInfos = new Gson().fromJson(result.getJSONArray("data").toString(), new TypeToken<List<GoodsInfo>>() {
                        }.getType());
                        if (recyclerView.isRefreshing()) {
                            differenceOrderAdapter.setNewData(newInfos);
                            if (newInfos.size() < pageSize) {
                                differenceOrderAdapter.setEnableLoadMore(false);
                            } else {
                                differenceOrderAdapter.setEnableLoadMore(true);
                            }
                        } else {
                            differenceOrderAdapter.addData(newInfos);
                            differenceOrderAdapter.loadMoreComplete();
                            if (newInfos.size() < pageSize) {
                                differenceOrderAdapter.loadMoreEnd();
                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    @Override
    protected void onFinish(HttpTag httpTag) {
        super.onFinish(httpTag);
        recyclerView.setRefresh(false);
    }
}
